package wiiusej.apptest.softwareControllers;

import java.util.Vector;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import wiiusej.Wiimote;
import wiiusej.apptest.activityTasks.EnergyTransmisionTask;
import wiiusej.apptest.activityTasks.RealTimeFeedback;
import wiiusej.apptest.activityTasks.Spell;
import wiiusej.apptest.activityTasks.SpellGameTask;
import wiiusej.apptest.statistics.AccelerationPaquet;





public class SoftwareControllerImpl implements SoftwareController{	
	
	//COM queues - Software controller
	private static BlockingQueue<Vector<AccelerationPaquet>> realTimeQueue;
	private static BlockingQueue<Integer> realTimeToGameController;
	private static BlockingQueue<Integer> gameToEnergyQ;
	private static ConcurrentLinkedQueue<Integer> fromEnergyToGameQ;
	private static BlockingQueue<Spell> eneergyToComClient;
	
	//COM queues - Hardware controllers
	private static BlockingQueue<Integer> toRumbleQ;
	private static BlockingQueue<Integer> fromRealToSoundQ;
	private static BlockingQueue<Integer> toLEDQ;
	
	private static Wiimote myWiimote; 
	
	//Task Executor
	private static final int NTHREADS = 100;
	private static final ExecutorService executor = 
		Executors.newFixedThreadPool(NTHREADS);	
	
	public SoftwareControllerImpl(Wiimote wiimote, BlockingQueue<Vector<AccelerationPaquet>> realTimeQueue, 
			BlockingQueue<Integer> realTimeToGameController, BlockingQueue<Integer> fromRealToSoundQ, 
			BlockingQueue<Integer> gameToEnergy, BlockingQueue<Integer> toRumbleQ, 
			ConcurrentLinkedQueue<Integer> fromEnergyToGameQ, BlockingQueue<Spell> eneergyToComClient, 
			BlockingQueue<Integer> toLEDQ){
	
		this.myWiimote = wiimote;
		
		this.realTimeQueue = realTimeQueue;
		this.realTimeToGameController = realTimeToGameController;
		this.fromRealToSoundQ = fromRealToSoundQ;
		this.gameToEnergyQ = gameToEnergy;
		this.toRumbleQ = toRumbleQ;
		this.fromEnergyToGameQ = fromEnergyToGameQ;
		this.eneergyToComClient = eneergyToComClient;
		this.toLEDQ = toLEDQ;
		
		initComponents();
		
	}
	
	
	
	
	private void initComponents(){
		
		//REAL TIME FEEDBACK
		Runnable realTimeApp = new RealTimeFeedback(realTimeQueue, 
				realTimeToGameController, fromRealToSoundQ);
		executor.execute(realTimeApp);
		
		//GAME CONTROLLER
		Runnable gameController = 
			new SpellGameTask(realTimeToGameController, gameToEnergyQ, toRumbleQ, fromEnergyToGameQ, myWiimote);
		executor.execute(gameController);		
		
		//ENERGY TRANSMISION
		Runnable energyTransmision = 
			new EnergyTransmisionTask(myWiimote, gameToEnergyQ, eneergyToComClient, 
					toRumbleQ, toLEDQ, fromEnergyToGameQ);		
		executor.execute(energyTransmision);	
	}

}
